package com.stox.core.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;

import com.stox.core.model.exception.StoxException;
import com.stox.core.util.StringUtil;
import com.stox.resources.i18n.MessageSource;

public class Bar implements IBar{

	private static String format = "dd-MMM-yyyy";
	
	private String scripCode;
	private Date date;
	private double open;
	private double high;
	private double low;
	private double close;
	private double volume;
	private double last;
	private double previousClose;
	private double turnover;
	private double trades;
	
	//Transient
	private transient Scrip scrip;
	
	public String toString(){
		return new SimpleDateFormat(format).format(getDate())+","+
				getOpen()+","+
				getHigh()+","+
				getLow()+","+
				getClose()+","+
				getVolume()+","+
				getLast()+","+
				getPreviousClose()+","+
				getTurnover()+","+
				getTrades();
	}
	
	public static Bar fromString(String string, String scripCode){
		try {
			Bar bar = new Bar();
			StringTokenizer tokenizer = new StringTokenizer(string, ",");
			bar.setScripCode(scripCode);
			bar.setDate(new SimpleDateFormat(format).parse(tokenizer.nextToken()));
			bar.setOpen(Double.parseDouble(tokenizer.nextToken()));
			bar.setHigh(Double.parseDouble(tokenizer.nextToken()));
			bar.setLow(Double.parseDouble(tokenizer.nextToken()));
			bar.setClose(Double.parseDouble(tokenizer.nextToken()));
			bar.setVolume(Double.parseDouble(tokenizer.nextToken()));
			bar.setLast(Double.parseDouble(tokenizer.nextToken()));
			bar.setPreviousClose(Double.parseDouble(tokenizer.nextToken()));
			bar.setTurnover(Double.parseDouble(tokenizer.nextToken()));
			bar.setTrades(Double.parseDouble(tokenizer.nextToken()));
			return bar;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public void validate(){
		if(!StringUtil.hasText(getScripCode())){
			throw new StoxException(MessageSource.getMessage("Scrip.code.for.bar.is.null.or.empty"));
		}
		if(getDate() == null){
			throw new StoxException(MessageSource.getMessage("Date.for.bar.is.null.or.empty"));
		}
		if(getOpen() == 0 || getHigh() == 0 || getLow() == 0 || getClose() == 0 || getVolume() == 0){
			throw new StoxException(MessageSource.getMessage("One.of.the.mandatory.values.for.bar.is.zero"));
		}
	}
	
	@Override
	public void format(){
		validate();
		setScripCode(getScripCode().trim().toUpperCase());
	}
	
	@Override
	public int compareTo(IBar o) {
		if(o != null && o.getDate() != null && getDate() != null){
			return ((-1)*getDate().compareTo(o.getDate()));
		}
		return 0;
	}
	
	@Override
	public Date getDate() {
		return date;
	}
	@Override
	public void setDate(Date date) {
		this.date = date;
	}
	@Override
	public double getOpen() {
		return open;
	}
	@Override
	public void setOpen(double open) {
		this.open = open;
	}
	@Override
	public double getHigh() {
		return high;
	}
	@Override
	public void setHigh(double high) {
		this.high = high;
	}
	@Override
	public double getLow() {
		return low;
	}
	@Override
	public void setLow(double low) {
		this.low = low;
	}
	@Override
	public double getClose() {
		return close;
	}
	@Override
	public void setClose(double close) {
		this.close = close;
	}
	@Override
	public double getVolume() {
		return volume;
	}
	@Override
	public void setVolume(double volume) {
		this.volume = volume;
	}

	@Override
	public String getScripCode() {
		return scripCode;
	}

	@Override
	public void setScripCode(String scripCode) {
		this.scripCode = scripCode;
	}

	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result
				+ ((scripCode == null) ? 0 : scripCode.hashCode());
		return result;
	}

	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Bar other = (Bar) obj;
		if (date == null) {
			if (other.date != null)
				return false;
		} else if (!date.equals(other.date))
			return false;
		if (scripCode == null) {
			if (other.scripCode != null)
				return false;
		} else if (!scripCode.equals(other.scripCode))
			return false;
		return true;
	}

	@Override
	public Scrip getScrip() {
		return scrip;
	}

	@Override
	public void setScrip(Scrip scrip) {
		this.scrip = scrip;
	}

	@Override
	public double getLast() {
		return last;
	}

	@Override
	public void setLast(double last) {
		this.last = last;
	}

	@Override
	public double getPreviousClose() {
		return previousClose;
	}

	@Override
	public void setPreviousClose(double previousClose) {
		this.previousClose = previousClose;
	}

	@Override
	public double getTurnover() {
		return turnover;
	}

	@Override
	public void setTurnover(double turnover) {
		this.turnover = turnover;
	}

	@Override
	public double getTrades() {
		return trades;
	}

	@Override
	public void setTrades(double trades) {
		this.trades = trades;
	}

}
